﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Runtime.InteropServices;

using DirectShowLib;

namespace SBPweb.MediaCenter.Modules.Organizer.WPF.Controls
{
    /// <summary>
    /// Interaction logic for FolderFileItem.xaml
    /// </summary>
    public partial class FolderFileItem : UserControl
    {

        #region Private fields

        private FileInfo file;

        #endregion

        #region Constructor logic

        public FolderFileItem()
        {
            InitializeComponent();
        }

        public FolderFileItem(FileInfo file)
        {
            InitializeComponent();
            File = file;
        }

        #endregion

        #region Property accessors

        public FileInfo File
        {
            get
            {
                return file;
            }
            set
            {
                bool b = file != value;
                file = value;
                if (b)
                {
                    PopulateItems();
                }
            }
        }

        #endregion

        #region Methods

        public void PopulateItems()
        {
            tbName.Text = file.Name;
            meThumb.Source = new Uri(file.FullName, UriKind.RelativeOrAbsolute);
            meThumb.Play();
            meThumb.Pause();
//            BitmapSource bmp = s(file);
//            this.Background = new System.Windows.Media.ImageBrush(bmp);
        }

        private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            meThumb.Play();
        }

        private void UserControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            meThumb.Pause();
        }

        //public BitmapSource s(FileInfo file)
        //{
        //    //IGraphBuilder graph = new FilterGraph() as IGraphBuilder;
        //    //graph.RenderFile(file.FullName, null);

        //    int hr;
        //    IFilterGraph2 filterGraph = new FilterGraph() as IFilterGraph2;
        //    ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
        //    lock (this)
        //    {
        //        try
        //        {
        //            hr = icgb2.SetFiltergraph(filterGraph);
        //            DsError.ThrowExceptionForHR(hr);

        //            IBaseFilter sourceFilter = null;
        //            hr = filterGraph.AddSourceFilter(file.FullName, file.FullName, out sourceFilter);
        //            DsError.ThrowExceptionForHR(hr);

        //            ISampleGrabber sampGrabber = (ISampleGrabber)new SampleGrabber();
        //            IBaseFilter baseGrabFlt = (IBaseFilter)sampGrabber;

        //            ConfigureSampleGrabber(sampGrabber);

        //            hr = filterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
        //            DsError.ThrowExceptionForHR(hr);

        //            hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null);
        //            DsError.ThrowExceptionForHR(hr);

        //            int width = 0;
        //            int height = 0;
        //            int stride = 0;
        //            int imageSize = 0;

        //            SaveSizeInfo(sampGrabber, out width, out height, out stride, out imageSize);

        //            IMediaControl mediaControl = filterGraph as IMediaControl;
        //            mediaControl.Run();

        //            IntPtr ipSnapShot = SnapShot(sampGrabber);

        //            mediaControl.Stop();

        //            return IPToBmp(ipSnapShot, width, height, stride);
        //            //SaveSizeInfo(sampGrabber);

        //            //IVideoWindow videoWindow = filterGraph as IVideoWindow;
        //            //ConfigureVideoWindow(videoWindow, hWin);
        //        }
        //        finally
        //        {
        //            if (icgb2 != null)
        //            {
        //                Marshal.ReleaseComObject(icgb2);
        //                icgb2 = null;
        //            }
        //        }
        //    }
        //}

        //public IntPtr SnapShot(ISampleGrabber grabber)
        //{
        //    int hr;
        //    IntPtr ip = IntPtr.Zero;
        //    int iBuffSize = 0;

        //    // Read the buffer size
        //    hr = grabber.GetCurrentBuffer(ref iBuffSize, ip);
        //    DsError.ThrowExceptionForHR(hr);

        //    //Debug.Assert(iBuffSize == m_ImageSize, "Unexpected buffer size");

        //    // Allocate the buffer and read it
        //    ip = Marshal.AllocCoTaskMem(iBuffSize);

        //    hr = grabber.GetCurrentBuffer(ref iBuffSize, ip);
        //    DsError.ThrowExceptionForHR(hr);

        //    return ip;
        //}

        //// Convert a point to the raw pixel data to a .NET bitmap
        //public BitmapSource IPToBmp(IntPtr ip, int width, int height, int stride)
        //{
        //    // We know the Bits Per Pixel is 24 (3 bytes) because we forced it 
        //    // to be with sampGrabber.SetMediaType()
        //    int iBufSize = width * height * 3;

        //    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(
        //        width,
        //        height,
        //        -stride,
        //        System.Drawing.Imaging.PixelFormat.Format24bppRgb,
        //        (IntPtr)(ip.ToInt32() + iBufSize - stride)
        //        );

        //    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
        //        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        //}

        //private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
        //{
        //    AMMediaType media;
        //    int hr;

        //    // Set the media type to Video/RBG24
        //    media = new AMMediaType();
        //    media.majorType = MediaType.Video;
        //    media.subType = MediaSubType.RGB24;
        //    media.formatType = FormatType.VideoInfo;
        //    hr = sampGrabber.SetMediaType(media);
        //    DsError.ThrowExceptionForHR(hr);

        //    DsUtils.FreeAMMediaType(media);
        //    media = null;

        //    // Configure the samplegrabber
        //    hr = sampGrabber.SetBufferSamples(true);
        //    DsError.ThrowExceptionForHR(hr);
        //}

        //private void SaveSizeInfo(ISampleGrabber sampGrabber, out int width, out int height, out int stride, out int imageSize)
        //{
        //    int hr;

        //    // Get the media type from the SampleGrabber
        //    AMMediaType media = new AMMediaType();

        //    hr = sampGrabber.GetConnectedMediaType(media);
        //    DsError.ThrowExceptionForHR(hr);

        //    try
        //    {

        //        if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
        //        {
        //            throw new NotSupportedException("Unknown Grabber Media Format");
        //        }

        //        // Get the struct
        //        VideoInfoHeader videoInfoHeader = new VideoInfoHeader();
        //        Marshal.PtrToStructure(media.formatPtr, videoInfoHeader);

        //        // Grab the size info
        //        width = videoInfoHeader.BmiHeader.Width;
        //        height = videoInfoHeader.BmiHeader.Height;
        //        stride = videoInfoHeader.BmiHeader.ImageSize / height;
        //        imageSize = videoInfoHeader.BmiHeader.ImageSize;
        //    }
        //    finally
        //    {
        //        DsUtils.FreeAMMediaType(media);
        //        media = null;
        //    }
        //}

        #endregion
    }

}